package com.mikey.pool;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author :Mikey
 * @description :自定义线程池
 *  new ThreadPoolExecutor.AbortPolicy() 线程池不处理，抛出异常
 *  new ThreadPoolExecutor.CallerRunsPolicy() 哪里的去哪里，
 *  new ThreadPoolExecutor.DiscardOldestPolicy（）队列满了不,尝试和最早的竞争，不会抛出异常
 *   new ThreadPoolExecutor.DiscardPolicy()  队列满了不会抛出异常，丢掉任务
 * @create :2022-09-22 09:47:00
 */
public class Demo1 {
    public static void main(String[] args) {
        //CPU密集型：几核就是几
        //IO密集型：大于判断程序中十分耗IO的线程数
        int i1 = Runtime.getRuntime().availableProcessors();//获取CPU核数
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                i1,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy());
        try {
            //java.util.concurrent.RejectedExecutionException,超出最大线程数，抛出异常
            for (int i = 0; i < 9; i++) {
                threadPoolExecutor.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPoolExecutor.shutdown();
        }
    }
}
